package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.dto.SetmealDTO;
import com.itheima.mapper.SetmealCheckGroupMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.SetmealService;
import com.itheima.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealCheckGroupMapper setmealCheckGroupMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 分页查询套餐
     *
     * @param queryPageBean
     * @return
     */
    public PageResult PageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        String queryString = queryPageBean.getQueryString();
        String code = null;
        String name = null;
        if (queryString != null && !queryString.isEmpty() && queryString.startsWith("0")) {

            //如果以0开头就是套餐编码
            code = queryString;
            Page<SetmealVO> page = setmealMapper.PageQuery(code, name);
            return new PageResult(page.getTotal(), page.getResult());

        } else if (queryString != null && !queryString.isEmpty()) {

            //不以0开头就是套餐名称
            name = queryString;
            Page<SetmealVO> page = setmealMapper.PageQuery(code, name);
            return new PageResult(page.getTotal(), page.getResult());

        } else {
            //如果查询条件为空
            Page<SetmealVO> page = setmealMapper.PageQuery(null, null);
            return new PageResult(page.getTotal(), page.getResult());
        }
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO, List<Integer> checkGroupIds) {
        //1.向套餐表插入套餐基本信息
        setmealMapper.addSetmeal(setmealDTO);

        // 把成功添加到数据库的套餐的图片名称保存到redis的一个set集合中
        String url = setmealDTO.getImg();
        //把图片名称从url中分割出来
        String imgName = url.substring(url.lastIndexOf("/") + 1);
        redisTemplate.opsForSet().add("SETMEAL_IMG_DB_RESOURCES", imgName);
        log.info("文件名称：{}",imgName);

        //2.向套餐-检查组中间表插入数据
        for (Integer checkGroupId : checkGroupIds) {
            setmealCheckGroupMapper.addCheckGroupToSetmeal(setmealDTO.getId(), checkGroupId);
        }
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    public SetmealVO findById(Integer id) {
        return setmealMapper.findById(id);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @param checkGroupIds
     */
    public void update(SetmealDTO setmealDTO, List<Integer> checkGroupIds) {
        //1.修改套餐基本信息
        setmealMapper.update(setmealDTO);
        //2.删除套餐与检查组关联关系
        setmealCheckGroupMapper.deleteGroupFromSetmeal(setmealDTO.getId());
        //3.添加套餐与检查组关联关系
        for (Integer checkGroupId : checkGroupIds) {
            setmealCheckGroupMapper.addCheckGroupToSetmeal(setmealDTO.getId(), checkGroupId);
        }

    }

    /**
     * 删除套餐
     * @param id
     */
    @Transactional
    public void delete(Integer id) {
        //1.删除套餐表数据
        setmealMapper.delete(id);

        //2.删除套餐与检查组关联关系
        setmealCheckGroupMapper.deleteGroupFromSetmeal(id);
    }
}
